package com.firesrc.web;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jodd.util.StringUtil;

import com.firesrc.bean.MailSubject;
import com.firesrc.service.MailSubjectService;
import com.firesrc.service.impl.MailSubjectServiceImpl;

/**
 * Servlet implementation class MailSubjectServlet
 */
public class MailSubjectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		MailSubjectService service = new MailSubjectServiceImpl();
		String type = request.getParameter("type");
		if ("add".equals(type)) {
			String name = request.getParameter("name");
			if (StringUtil.isNotEmpty(name)) {
				String state = request.getParameter("state");
				String kind = request.getParameter("kind");
				MailSubject subject = new MailSubject();
				subject.setId(UUID.randomUUID().toString());
				subject.setKind(kind);
				subject.setState(state);
				subject.setName(name);
				service.add(subject);
			}
		} else if ("delete".equals(type)) {
			String id = request.getParameter("id");
			service.delete(id);
		}
		List<MailSubject> list = service.getAllList();
		request.setAttribute("list", list);
		request.getRequestDispatcher("mailSubjectList.jsp").forward(request, response);

	}

}
